package org.example.c4;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.LongAccumulator;

/**
 * @author liu-rui
 * @date 2020/6/9 下午3:12
 * @description
 * @since
 */
@Slf4j
public class Demo10 {
    public static void main(String[] args) {
        LongAccumulator accumulator = new LongAccumulator(Long::max, 0);
        CountDownLatch latch = new CountDownLatch(8);

        for (int i = 0; i < 8; i++) {
            new Thread(() -> {
                long max = 0;

                for (int j = 0; j < 1000; j++) {
                    long val = ThreadLocalRandom.current().nextLong(6000);
                    accumulator.accumulate(val);
                    max = Long.max(val, max);
                }
                log.info("max {}", max);
                latch.countDown();
            }, "t" + i).start();
        }


        try {
            latch.await();
        } catch (InterruptedException e) {
        }
        log.info("ret:{}" , accumulator.get());
    }
}
